package Classes
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	public class World
	{
		public var entityManager:EntityManager = new EntityManager();
		
		private var m_display:Sprite = new Sprite();
		private var m_updateTimer:Timer = new Timer(25);
		private var m_width:Number = 5; //meters
		private var m_height:Number = 5; //meters;
		
		private var m_player:Entity;
		
		private var m_worldCells:Object = new Object;
		private var m_attachedMobs:Vector.<Entity> = new Vector.<Entity>;
		private var m_attachedPlayers:Vector.<Entity> = new Vector.<Entity>;
		
		public function World()
		{
			entityManager.setWorld(this);
			var worldCell:WorldCell;
			var tf:TextField;
			
			var bg:Sprite = new Sprite();
			bg.graphics.beginFill(0xAAAADD);
			bg.graphics.drawRect(0,0,m_width*Main.P2WSCALE,m_height*Main.P2WSCALE);
			bg.graphics.endFill();
			
			for(var i:int=0;i<m_width;i++){
				m_worldCells[i] = new Object();
				for(var j:int=0;j<m_height;j++){
					worldCell = new WorldCell(this,new Point(i,j));
					worldCell.setSpawnRule(new SpawnRule);
					m_worldCells[i][j] = worldCell;
					
					tf = new TextField();
					tf.text = i+','+j;
					tf.x = i*Main.P2WSCALE;
					tf.y = j*Main.P2WSCALE;
					tf.alpha = .5;
					tf.selectable = false;
					tf.setTextFormat(new TextFormat('courier',10));
					bg.addChild(tf);
				}
			}

			m_display.addChild(bg);	
			
			m_updateTimer.addEventListener(TimerEvent.TIMER, onTimerTick);
			m_updateTimer.start();	
		}
		
		
		public function getPlayer():Entity{
			return m_player;
		}
		
		public function getWidth():Number{
			return m_width;
		}
		
		public function getHeight():Number{
			return m_height;
		}
		
		public function setPlayer(entity:Entity):void{
			m_player = entity;
			m_player.getRigidBodyComponent().attach(this);
		}

		public function getDisplay():Sprite{
			return m_display;
		}
		
		private function onTimerTick(te:TimerEvent):void{
			update();
		}
		
		private function update():void{
			var i:int;
			
			if(Main.actionManager.hasActions()){
				Main.actionManager.processActions();
			}

			Main.componentManager.update();		
		}

		public function attach(entity:Entity):void{
			if(entity.hasTag('mob')){
				m_attachedMobs.push(entity);
			}
			
			if(entity.hasTag('player')){
				m_attachedPlayers.push(entity);
			}
			
			entityManager.addEntity(entity);
		}
		
		public function detach(entity:Entity):void{
			if(entity.hasTag('mob')){
				m_attachedMobs.splice(m_attachedMobs.indexOf(entity),1);
			}
			
			if(entity.hasTag('player')){
				m_attachedPlayers.splice(m_attachedPlayers.indexOf(entity),1);
			}
			
			entityManager.removeEntity(entity);
		}
		
		public function getAttachedMobs():Vector.<Entity>{
			return m_attachedMobs;
		}
		
		public function getAttachedPlayers():Vector.<Entity>{
			return m_attachedPlayers;
		}
	}
}